package pos.data;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import pos.domain.*;
import pos.data.*;
import pos.util.UIDGenerator;

public class JDBCDetallesDAO implements IDetallesDAO{
	IEventoDAO pdao;
    
    public JDBCDetallesDAO() {
        pdao = new JDBCEventoDAO();
    }

	    public void delete(Connection conn, String DetalleOID) {
	        String sql = "DELETE FROM Detalle WHERE (calendarioOID = ?) ";
	        PreparedStatement stmt = null;

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, DetalleOID);
	            stmt.executeUpdate();
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (stmt != null)
	                    stmt.close();
	            } catch (SQLException e) {
	            }
	        }
	    }

	    public void insert(Connection conn, List Detalle, String calendarioOID) {

	        String sql = "INSERT INTO Detalle ( OID, calendarioOID, eventoOID, comentarios, fecha, franjaHoraria) VALUES (?, ?, ?, ?, ?, ?) ";
	        PreparedStatement stmt = null;

	        try {
	            stmt = conn.prepareStatement(sql);
	            
	            for (Iterator it = Detalle.iterator(); it.hasNext();) {
	                Detalle d = (Detalle) it.next();
	                stmt.clearParameters();
	                stmt.setString(1, UIDGenerator.getInstance().getKey());
	                stmt.setString(2, calendarioOID);
	                String oidp = pdao.selectEventoOID(conn,d.getEv().getIdEvento());
	                stmt.setString(3, oidp);
	                stmt.setString(4, d.getComentarios());
	                stmt.setDate(5, (Date) d.getFecha());
	                stmt.setString(6, d.getFranjaHoraria());
	                
	                stmt.executeUpdate();
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	    }

	    public List select(Connection conn, String calendarioOID) {
	        List Detalle = new LinkedList();
	        String sql = "SELECT * FROM Detalle WHERE (ORDEROID = ? ) ";
	        PreparedStatement stmt = null;
	        ResultSet result = null;

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, calendarioOID);

	            result = stmt.executeQuery();

	            while (result.next()) {
	                Detalle d = new Detalle();
	                 d.setComentarios(result.getString("comentario"));
	                 d.setFecha(result.getDate("fecha"));
	                 d.setFranjaHoraria(result.getString("franja"));
	               
	                evento p = pdao.select(conn, result.getString("eventoOID"));
	                d.setEv(p);
	                Detalle.add(d);
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }

	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }

	        return Detalle;
	    }

		
}
